/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package visao;
import bean.Estabelecimento;
import java.util.logging.Level;
import java.util.logging.Logger;
import negocio.GerenciarDB;

/**
 *
 * @author Leandro
 */
public class JanelaInicioEst extends javax.swing.JFrame {
    private Estabelecimento estabelecimento;
    /**
     * Creates new form JanelaInicioEst
     */
    public JanelaInicioEst() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        iCards = new javax.swing.JLabel();
        CNPJdaLoja = new javax.swing.JLabel();
        SenhaLoja = new javax.swing.JLabel();
        BotaoLogin = new javax.swing.JButton();
        CNPJLoja = new javax.swing.JFormattedTextField();
        SenhadaLoja = new javax.swing.JPasswordField();
        ErroAcessoEst = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Login - Estabelecimento");
        setMinimumSize(new java.awt.Dimension(690, 410));
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        iCards.setFont(new java.awt.Font("Tahoma", 1, 36)); // NOI18N
        iCards.setText("iCards");
        getContentPane().add(iCards, new org.netbeans.lib.awtextra.AbsoluteConstraints(299, 34, -1, -1));

        CNPJdaLoja.setText("Digite o CNPJ da loja:");
        getContentPane().add(CNPJdaLoja, new org.netbeans.lib.awtextra.AbsoluteConstraints(240, 130, 140, 30));

        SenhaLoja.setText("Digite a senha:");
        getContentPane().add(SenhaLoja, new org.netbeans.lib.awtextra.AbsoluteConstraints(240, 190, 140, 30));

        BotaoLogin.setText("Entrar");
        BotaoLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BotaoLoginActionPerformed(evt);
            }
        });
        getContentPane().add(BotaoLogin, new org.netbeans.lib.awtextra.AbsoluteConstraints(310, 270, 100, 30));

        try{  
            javax.swing.text.MaskFormatter data= new javax.swing.text.MaskFormatter("##.###.###/####-##");  
            CNPJLoja = new javax.swing.JFormattedTextField(data);  
        }  
        catch (Exception e){  
        }
        CNPJLoja.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CNPJLojaActionPerformed(evt);
            }
        });
        getContentPane().add(CNPJLoja, new org.netbeans.lib.awtextra.AbsoluteConstraints(380, 130, 117, 30));
        getContentPane().add(SenhadaLoja, new org.netbeans.lib.awtextra.AbsoluteConstraints(380, 190, 117, 30));

        ErroAcessoEst.setForeground(new java.awt.Color(255, 0, 0));
        getContentPane().add(ErroAcessoEst, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 230, 215, 22));

        setSize(new java.awt.Dimension(728, 514));
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void BotaoLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BotaoLoginActionPerformed
        // TODO add your handling code here:
        GerenciarDB banco = new GerenciarDB();
        String cnpj = CNPJLoja.getText().replaceAll("[.]","");
        cnpj = cnpj.replaceAll("[-]","");
        cnpj = cnpj.replaceAll("[/]","");
        String senha = SenhadaLoja.getText();
        double saldo;
        
        try { 
            String nome_estab = banco.getEstabelecimentoDB(cnpj);        
            this.estabelecimento = new Estabelecimento(cnpj, nome_estab);
            boolean acesso = banco.checkSenhaEstabelecimentoDB(cnpj, senha);       
                        
            if (acesso){
                saldo = banco.getVendaTotalEstabelecimentoDB(cnpj);
                JanelaEst frame = new JanelaEst();
                frame.estabelecimento = this.estabelecimento; 
                frame.CNPJ = cnpj;
                if (saldo<=50) {
                    frame.Saldo.setForeground(new java.awt.Color(255, 0, 0));
                    frame.jLabel1.setText("AVISO: Sua receita atual é inferior a R$ 50.00.");
                    }
                    frame.Saldo.setText("R$ "+saldo+"0");
                frame.startJanelaEst(cnpj);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
                this.dispose();
            }
            else{
                ErroAcessoEst.setText("Número do CNPJ ou senha incorreta.");
            }
            
        } catch (Exception ex) {
            Logger.getLogger(JanelaInicioEst.class.getName()).log(Level.SEVERE, null, ex);
        }             
        
    }//GEN-LAST:event_BotaoLoginActionPerformed

    private void CNPJLojaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CNPJLojaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_CNPJLojaActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JanelaInicioEst.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JanelaInicioEst.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JanelaInicioEst.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JanelaInicioEst.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new JanelaInicioEst().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BotaoLogin;
    private javax.swing.JFormattedTextField CNPJLoja;
    private javax.swing.JLabel CNPJdaLoja;
    private javax.swing.JLabel ErroAcessoEst;
    private javax.swing.JLabel SenhaLoja;
    private javax.swing.JPasswordField SenhadaLoja;
    private javax.swing.JLabel iCards;
    // End of variables declaration//GEN-END:variables
}
